package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.aizuda.limiter.annotation.RateLimit;
import com.aizuda.limiter.strategy.IpKeyGenerateStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.constant.ResponseWord;
import com.pyue.eam.admin.api.entity.regi.CqTaskMsgEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.common.security.util.SecurityUtils;
import com.pyue.eam.regi.service.CqTaskMsgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * 任务消息
 *
 * @date 2023-11-17 15:34:32
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqTaskMsg")
@Tag(description = "cqTaskMsg", name = "任务消息管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqTaskMsgController {

    private final CqTaskMsgService cqTaskMsgService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param cqTaskMsg 任务消息
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R getCqTaskMsgPage(@ParameterObject Page page, @ParameterObject CqTaskMsgEntity cqTaskMsg) {
        if (page.getSize() <= 20) {
            page.setSize(200);
        }
        ApiAsserts.isTrue(cqTaskMsg.getTaskId() != null, "传参错误");

        LambdaQueryWrapper<CqTaskMsgEntity> wrapper = Wrappers.lambdaQuery(cqTaskMsg);
        Page<CqTaskMsgEntity> dataPage = cqTaskMsgService.page(page, wrapper);
        if (CollUtil.isNotEmpty(dataPage.getRecords())) {
            String userId = Convert.toStr(SecurityUtils.getUserId(), "");

            dataPage.getRecords().forEach(e -> {
                if (userId.equals(e.getCreateBy())) {
                    e.setSelfFlag("1");
                }
            });
        }

        return R.ok(dataPage);
    }

    /**
     * 新增任务消息
     *
     * @param cqTaskMsg 任务消息
     * @return R
     */
    @Operation(summary = "新增任务消息", description = "新增任务消息")
    @SysLog("新增任务消息")
    @PostMapping
    @RateLimit(count = 1, interval = "3s", strategy = {IpKeyGenerateStrategy.TYPE}, message = ResponseWord.WAIT_TIME)
    public R<Long> save(@RequestBody CqTaskMsgEntity cqTaskMsg) {
        return R.ok(cqTaskMsgService.saveData(cqTaskMsg));
    }

    /**
     * 通过id删除任务消息
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除任务消息", description = "通过id删除任务消息")
    @SysLog("通过id删除任务消息")
    @DeleteMapping
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(cqTaskMsgService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
